// pages/shop_order/shop_order.js
const api = require('../../utils/util.js');
const service = require('../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopData: '',
    showListTag: false,
    activeIndex: 0,
    activeUpIndex: 0,
    orderNumber: {
      unpay_num: '',
      pay_num: '',
      express_num: '',
      refund_num: '',
      comment_num: '',
      finish_num: ''
    },
    orderList: [],
    order_state: 1,
    page: 1,
    shopId: '',
    tem_order_id: '',
    refundPrice: '',
    order_return_id: '',
    yzc_status: '',
    deal_confirm: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  orderListGet: function(scrolltobottom) {
    var that = this;

    api.ajax(service.url('orderListGet'), {
        unionid: this.data.shopData.shop_unionid,
        token: this.data.shopData.token,
        token_key: this.data.shopData.token_key,
        page: this.data.page,
        order_state: this.data.order_state
      },
      function(res) {
        if (res.code) {
          api.toast(res.message)
        } else {
          if (res.data != '') {
            console.log(res.data);
            if (!scrolltobottom) {
              that.data.orderList = [];
            }
            that.data.orderList = that.data.orderList.concat(res.data.list);

            if (res.data.list.length > 0) {
              that.setData({
                orderList: that.data.orderList,
                showListTag: true
              })
            } else {
              if (!scrolltobottom) {
                that.setData({
                  showListTag: false
                })
                api.toast('该状态下没有任何订单');
              } else {
                if (that.page != 1) {
                  that.setData({
                    page: parseInt(that.data.page - 1)
                  })
                  api.toast('没有更多数据...');
                }
              }

            }

            that.setData({
              ['orderNumber.unpay_num']: res.data.unpay_num,
              ['orderNumber.pay_num']: res.data.pay_num,
              ['orderNumber.express_num']: res.data.express_num,
              ['orderNumber.refund_num']: res.data.refund_num,
              ['orderNumber.comment_num']: res.data.comment_num,
              ['orderNumber.finish_num']: res.data.finish_num
            })

            console.log("获取商品列表成功!");
          } else if (res.data == '') {
            that.orderList = [];
            api.toast({
              msg: '该状态下没有任何订单',
              duration: 2000,
              location: 'middle'
            });
          }
        }
      });
  },

  //搜索事件
  openSearch: function(e) {
    this.setData({
      searchName: e.detail.value
    })
    wx.navigateTo({
      url: './order_search/order_search?searchName=' + e.detail.value,
    })
  },

  //--状态切换
  upOrderTab: function(e) {
    var that = this;
    let indes = e.currentTarget.dataset.inds;

    if (indes == '-1') {
      return;
    } else if (indes == 0) { //--进行中
      // $(".shop_order_list").css("padding-top", "11rem");
      // $(".shop_order_null").css("padding-top", "13rem");

      this.setData({
        activeIndex: 0, //点击“进行中”的时候默认选中待付款
        order_state: 1 //点击“进行中”的时候默认获取待付款订单
      })
    } else if (indes == 1) { //--已完成
      // $(".shop_order_list").css("padding-top", "11rem");
      // $(".shop_order_null").css("padding-top", "13rem");

      this.setData({
        activeIndex: 4, //点击“已完成”的时候默认选中待评价
        order_state: 4 //点击“已完成”的时候默认获取待评价订单
      })
    } else if (indes == 2) { //--已关闭
      // //--已关闭，隐藏订单状态统计框
      // $(".shop_order_list").css("padding-top", "7.5rem");
      // $(".shop_order_null").css("padding-top", "12rem");

      this.setData({
        order_state: 6 //点击“已关闭”的时候获取已关闭订单
      })
    } else if (indes == 3) { //--已退款/退货
      // //--已退款/退货，隐藏订单状态统计框
      // $(".shop_order_list").css("padding-top", "7.5rem");
      // $(".shop_order_null").css("padding-top", "12rem");

      this.setData({
        order_state: 8 //点击“已关闭”的时候获取已关闭订单
      })
    }

    this.setData({
      activeUpIndex: indes, //点击“已关闭”的时候获取已关闭订单
      page: 1,
    })

    //--获取已关闭订单列表
    var scrolltobottom = 0;
    this.orderListGet(scrolltobottom);
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  //--状态切换
  orderTab: function(e) {
    var that = this;
    let indes = e.currentTarget.dataset.inds;
    if (indes == '-1') {
      return;
    } else if (indes == 0) {
      this.data.order_state = 1;
    } else if (indes == 1) {
      this.data.order_state = 2;
    } else if (indes == 2) {
      this.data.order_state = 3;
    } else if (indes == 3) {
      this.data.order_state = 7;
    } else if (indes == 4) {
      this.data.order_state = 4;
    } else if (indes == 5) {
      this.data.order_state = 5;
    }

    this.setData({
      order_state: this.data.order_state,
      page: 1,
      activeIndex: indes
    })

    //--改变orderList的数据
    var scrolltobottom = 0;
    this.orderListGet(scrolltobottom);

    wx.pageScrollTo({
      scrollTop: 0
    }) //--回到顶部
  },

  //打开订单详情
  getOrderDetail: function(e) {
    let order_id = e.currentTarget.dataset.orderId;
    wx.navigateTo({
      url: '../order_detail_seller/order_detail_seller?order_id=' + order_id,
    })
  },

  //--底下按钮点击
  orderModBtn: function(e) {
    var that = this;
    let mod = e.currentTarget.dataset.mod;
    let order_id = e.currentTarget.dataset.orderId;

    switch (mod) {
      case "shopChat":
        wx.showModal({
          title: '提示',
          content: '下载APP更方便联系买家，是否前往下载?',
          success: function(rel) {
            if (rel.confirm) {
              wx.navigateTo({
                url: '../download_app/download_app',
              })
            }
          }
        })
        break;
      case "shopSend":
        //--发货
        wx.navigateTo({
          url: '../shop_send/send?order_id=' + order_id,
        })
        break;
      case "shopDel":
        //--确认是否删除订单
        wx.showModal({
          title: '提示',
          content: '是否删除订单?',
          success: function(rel) {
            if (rel.confirm) {
              api.ajax(service.url('sellerorderDelOrder'), {
                  unionid: that.data.shopData.shop_unionid,
                  token: that.data.shopData.token,
                  token_key: that.data.shopData.token_key,
                  order_id: order_id
                },
                function(res) {
                  if (res.code) {
                    api.toast(res.message);
                  } else {
                    var scrolltobottom = 0;
                    that.orderListGet(scrolltobottom);

                    api.toast('订单已删除');
                  }
                });
            }
          }
        });
        break;
      case "shopFlow":
        //--查看物流
        wx.navigateTo({
          url: '../shop_flow/flow?order_id=' + order_id,
        })
        break;
      case "shopRefundRefuse":
        //--商家拒绝退款
        wx.navigateTo({
          url: '../refuse_refun/refuse_refun?order_id=' + order_id,
        })
        break;
      case "shopRefundPass":
        // 同意申请
        this.getOrderDetailInfo(order_id, function(res) {
          console.log(res);

          wx.showModal({
            title: '提示',
            content: '是否同意退货退款申请?',
            success: function(rel) {
              if (rel.confirm) {
                api.ajax(service.url('agreeReturnOrder'), {
                  unionid: that.data.shopData.shop_unionid,
                  token: that.data.shopData.token,
                  token_key: that.data.shopData.token_key,
                  order_return_id: res.data.refund.order_return_id
                }, function(res) {
                  if (res.code == 0) {
                    var scrolltobottom = 0;
                    that.orderListGet(scrolltobottom);
                    api.toast('操作成功');

                  } else {
                    api.toast(res.message)
                  }
                })
              }
            }
          })
        });
        break;
      default:
        break;
    }
  },

  //获取订单详情信息
  getOrderDetailInfo: function(order_id, callback) {
    api.ajax(service.url('sellerOrderDetail'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      order_id: order_id
    }, function(res) {
      if (res.code == 0) {
        callback(res);
      } else {
        api.toast(res.message)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
		this.setData({
      shopData: wx.getStorageSync('shop_data_shop')
    })
    this.orderListGet();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.setData({
      page: parseInt(this.data.page + 1)
    })
    var scrolltobottom = 1;
    this.orderListGet(scrolltobottom);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})